package basic.datastruct.List;

import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @创建人 MBY381
 * @创建时间 2022/4/21 14:47
 * @描述 暂无
 */
public class CopyOnWriteLi {
    public static void main(String[] args) {
        test();

    }

    public static void test(){
        CopyOnWriteArrayList<Integer> li=new CopyOnWriteArrayList<>();
        CopyOnWriteArraySet<Integer> li2=new CopyOnWriteArraySet<>();
        Thread thread1=new Thread(()->{
            li.add(3);
            li.add(4);
            System.out.println("?");
            System.out.println(li);
            li2.add(1);
            li2.add(2);
            System.out.println(Thread.currentThread().getName()+" "+li2);
        });
        Thread thread2=new Thread(()->{
            ReentrantLock lock=new ReentrantLock();
            lock.lock();
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            li.add(7);
            li.add(6);
            System.out.println("!");
            System.out.println(li);
            li2.add(1);
            li2.add(2);
            System.out.println(Thread.currentThread().getName()+" "+li2);
            lock.unlock();
        });
        thread1.start();
        thread2.start();
    }
}
